from etgui import *
from PageLayout import *
from ListEditor import AbstrListEditor 
from TextEditor import MakeEditorDialog, SimpleTextEditor
import CmdNo
import os,string

class ListSelector(AbstrListEditor):
	def __init__(self, selectAction=None):
		self.selectAction = selectAction
		self.items=[]
		AbstrListEditor.__init__(self)
	def DoMakeItem(self, item):
		return TextItem(-1, str(item))
	def DoSetItems(self,items):
		self.items=items
		return items
	def DoGetItems(self):
		return self.items
	def DoSelect(self, i):
		if self.selectAction:
			self.SelectAction()
	def DoGetButtonConstructors(self):
		return []

def MakeTextInputView(label='Enter Text:', *items):
	args = (eVObjHExpand|eVObjVExpand,)
	args = args + items
	args = args + (
		TextItem(-1, label, gFixedFont), 
		PotGuiTextView("text",TextView(),Point(200, 150))
	)
	view=apply(MakePageLayout, args)
	return view

def MakeTitle(action,files):
	title = action
	files = string.split(files)
	cnt = len(files)
	if cnt==1:
		title = title + ' ' + os.path.basename(files[0])
	else:
		title = title + ' %s file%s' % (cnt,(('','s')[cnt>1]))
	return title

def MakeCheckOutDialog(files):
	reserved = PotGuiOneOf("comode", align=eVObjHLeft)
	reserved.AddItem("Reserved", "reserved")
	reserved.AddItem("Unreserved", "unreserved")
	view = MakePageLayout(
		eVObjHExpand|eVObjVExpand,
		reserved,
		TextItem(-1, 'Comment:', gFixedFont), 
		PotGuiTextView("comment",TextView(),Point(200, 150))
	)
	title = MakeTitle("CheckOut",files)
	return MakeOkCancelDialog(title, PotGuiModel(), view)

def MakeCheckInDialog(files):
	view = MakePageLayout(
		eVObjHExpand|eVObjVExpand,
		TextItem(-1, 'Comment:', gFixedFont), 
		PotGuiTextView("comment",TextView(),Point(200, 150))
	)
	title = MakeTitle("CheckIn",files)
	return MakeOkCancelDialog(title, PotGuiModel(), view)

def ShowCheckOutDialog(files):
	dialog=MakeCheckOutDialog(files)
	model=dialog.GetModel()
	model.SetValue("comment","")
	model.SetValue("comode","reserved")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("comment"), model.GetValue("comode")=="reserved"
	return None

def ShowCheckInDialog(files):
	dialog=MakeCheckInDialog(files)
	model=dialog.GetModel()
	model.SetValue("comment","")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("comment")
	return None

def ShowAddToSourceDialog(file):
	view = MakeTextInputView('Enter Creation Comment:')
	dialog = MakeOkCancelDialog("Add To Source Control", PotGuiModel(), view)
	model = dialog.GetModel()
	model.SetValue("text","")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("text")
	return None

def ShowMakeBranchDialog(file):
	branchfield = Form(
		items=[
			"Branch:", PotTextField("branch")
		]
	)
	view = MakeTextInputView('Enter Creation Comment:', branchfield)
	dialog = MakeOkCancelDialog("Make Branch", PotGuiModel(), view)
	model = dialog.GetModel()
	model.SetValue("text","")
	model.SetValue("branch","")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("text"), model.GetValue("branch")
	return None

def ShowMakeLabelDialog(file):
	labelfield = Form(
		items=[
			"Label:", PotTextField("label")
		]
	)
	view = MakeTextInputView('Enter Creation Comment:', labelfield)
	dialog = MakeOkCancelDialog("Make Label", PotGuiModel(), view)
	model = dialog.GetModel()
	model.SetValue("text","")
	model.SetValue("label","")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("text"), model.GetValue("label")
	return None

def ShowMakeAttributeDialog(file):
	labelfield = Form(
		items=[
			"Name:", PotTextField("name"),
			"Value:", PotTextField("value")
		]
	)
	view = MakeTextInputView('Enter Creation Comment:', labelfield)
	dialog = MakeOkCancelDialog("Make Attribute", PotGuiModel(), view)
	model = dialog.GetModel()
	model.SetValue("text","")
	model.SetValue("label","")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("text"), model.GetValue("name"), model.GetValue("value")
	return None

def ShowMakeDirectoryDialog():
	dirfield = Form(
		items=[
			"Location:", PotGuiDirSelector("parent", gTextFieldLen, "Directory"),
			"Directory:", PotTextField("dir")
		]
	)
	view = MakeTextInputView('Enter Creation Comment:', dirfield)
	dialog = MakeOkCancelDialog("Make Directory", PotGuiModel(), view)
	model = dialog.GetModel()
	model.SetValue("text","")
	model.SetValue("parent", gSystem.WorkingDirectory())
	model.SetValue("dir","")
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("text"), model.GetValue("parent"), model.GetValue("dir")
	return None

def ShowSelectViewDialog(views):
	list = ListSelector()
	list.SetItems(views)
	view = MakeListEditorLayout(list)
	dialog = MakeOkCancelDialog("Select View", PotGuiModel(), view)
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		idx = list.GetSelected()
		r = None
		if idx >= 0:
			return views[idx]
	return None

gCSEditor = None
def ShowConfigSpecEditor(cs):
	global gCSEditor
	if not gCSEditor:
		gCSEditor=MakeEditorDialog(SimpleTextEditor, title='config_spec',flags=eWinBlock)
	model = gCSEditor.GetModel()
	model.SetValue("Text",cs)
	rc = gCSEditor.ShowUnderMouse()
	if rc == CmdNo.cIdCancel:	
		return None
	return model.GetValue("Text")

def ShowLabelOnExeDialog():
	view = MakePageLayout(eVObjHExpand|eVObjVExpand,
		Form(
			items = [
				"Enter the Label:", PotTextField("text")
			]
		),
		PotGuiToggleButton("replace","Replace existing Label")
	)
	dialog = MakeOkCancelDialog("Label on Executable", PotGuiModel(), view)
	model = dialog.GetModel()
	model.SetValue("text","")
	model.SetValue("replace",0)
	if dialog.ShowUnderMouse()==CmdNo.cIdOk:
		return model.GetValue("text"), model.GetValue("replace")
	return None

